document.addEventListener('DOMContentLoaded', function () {
const generateButton = document.getElementById('generateButton');
const findDivisibleButton = document.getElementById('findDivisibleButton');
const resetButton = document.getElementById('resetButton');
const resultTextarea = document.getElementById('resultTextarea');
let numbers = [];

generateButton.addEventListener('click', function () {
numbers = [];
for (let i = 0; i < 20; i++) {
const randomNumber = Math.floor(Math.random() * 9000) + 1000;
numbers.push(randomNumber);
}
numbers.sort((a, b) => a - b);
let result = "随机产生20个4位整数，分别如下：\n";
result += numbers.join(', ');
result += "\n从小到大排序后结果为：\n";
result += numbers.join(', ');
resultTextarea.value = result;
});

findDivisibleButton.addEventListener('click', function () {
if (numbers.length === 0) {
alert('请先点击“随机产生20个整数”按钮');
return;
}
const divisibleBy5 = numbers.filter(num => num % 5 === 0);
let result = "能被5整除的整数有：\n";
result += divisibleBy5.join(', ');
resultTextarea.value += "\n" + result;
});

resetButton.addEventListener('click', function () {
resultTextarea.value = '';
numbers = [];
});
});